<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Product extends Controller
{
    protected $middleware = [
        'CheckAdmin'
    ];

    public function lists(Request $request)
    {
        if ($request->isAjax()) {
            $page = $request->get('page',1,'intval');
            $name = $request->get('name', '', 'trim');
            $page = $page > 0 ? $page : 1;
            $limit = $request->get('limit',10,'intval');
            $product = Db('product');
            if ($name !== '') {
                $product->where('name', 'like', '%' . $name . '%');
            }
            $count = $product->count();
            $list = $product
                ->field('product_id,name,image,type,price,money,deposit,create_time')
                ->page($page, $limit)
                ->order('product_id desc')
                ->select();
            return ['code' => 0, 'data' => $list, 'count' => $count];
        } else {
            return $this->fetch();
        }
    }

    public function addProduct(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $data['name'] = $request->post('name', '', 'trim');
            $data['image'] = $request->post('product_image', '', 'trim');
            $data['price'] = $request->post('price', 0, 'floatval');
            $data['money'] = $request->post('money', 0, 'floatval');
            $data['deposit'] = $request->post('deposit', 0, 'floatval');
            $data['medal_id'] = $request->post('medal_id', 0, 'intval');
            $data['type'] = $request->post('type', 1, 'intval');
            $data['validity_day'] = $request->post('validity_day', 0, 'intval');
            $data['description'] = $request->post('description', '', 'trim');
            $data['content'] = $request->post('content', '', 'trim');
            $data['create_time'] = date('Y-m-d H:i:s');
            if ($data['name'] == '') {
                return ['code' => 1, 'msg' => '请填写产品名称'];
            }
            if ($data['price'] == 0) {
                return ['code' => 1, 'msg' => '请填写产品价格'];
            }
            if ($data['deposit'] == 0) {
                return ['code' => 1, 'msg' => '请填写产品定金价格'];
            }
            if (false === Db('product')->insert($data)) {
                return ['code' => 1, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            $medal = Db('medal')->select();
            $this->assign('medal', $medal);
            return $this->fetch();
        }
    }

    public function editProduct(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $product_id = $request->post('product_id', 0, 'intval');
            $data['name'] = $request->post('name', '', 'trim');
            $data['image'] = $request->post('product_image', '', 'trim');
            $data['price'] = $request->post('price', 0, 'floatval');
            $data['money'] = $request->post('money', 0, 'floatval');
            $data['deposit'] = $request->post('deposit', 0, 'floatval');
            $data['type'] = $request->post('type', 1, 'intval');
            $data['medal_id'] = $request->post('medal_id', 0, 'intval');
            $data['validity_day'] = $request->post('validity_day', 0, 'intval');
            $data['description'] = $request->post('description', '', 'trim');
            $data['content'] = $request->post('content', '', 'trim');
            if ($data['name'] == '') {
                return ['code' => 1, 'msg' => '请填写产品名称'];
            }
            if ($data['price'] == 0) {
                return ['code' => 1, 'msg' => '请填写产品价格'];
            }
            if ($data['deposit'] == 0) {
                return ['code' => 1, 'msg' => '请填写产品定金价格'];
            }
            if (false === Db('product')->where('product_id',$product_id)->update($data)) {
                return ['code' => 1, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            $product_id = $request->get('id', 0, 'intval');
            $info = Db('product')->where('product_id',$product_id)->find();
            $this->assign('info', $info);
            $medal = Db('medal')->select();
            $this->assign('medal', $medal);
            return $this->fetch();
        }
    }

    public function deleteProduct(Request $request)
    {
        $product_id = $request->get('id', 0, 'intval');
        if (false === Db('product')->where('product_id', $product_id)->delete()) {
            return ['code' => 1, 'msg' => '操作失败'];
        } else {
            return ['code' => 0, 'msg' => '操作成功'];
        }
    }
}